package cn.sdormitory.controller.smartdor;

import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.dormitoryHygiene.req.DormitoryHygieneListReq;
import cn.sdormitory.dormitoryHygiene.vo.DormitoryHygieneCountVO;
import cn.sdormitory.dormitoryHygiene.vo.ExportDormitoryHygieneVO;
import cn.sdormitory.smartdor.service.DormitoryHygieneService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author : yy
 * @date: 2024/9/24 15:02
 * @description:
 */
@RestController
@Api(tags = "宿舍卫生")
@RequestMapping("/smartdor/dorHygiene")
public class DormitoryHygieneController {
    @Resource
    private DormitoryHygieneService dormitoryHygieneService;

    @ApiOperation("查询宿舍卫生列表")
    @PostMapping(value = "/listDormitoryHygiene")
    public CommonResult<CommonPage<DormitoryHygieneCountVO>> listDormitoryHygiene(@RequestBody DormitoryHygieneListReq hygieneListReq) {
        IPage<DormitoryHygieneCountVO> page = dormitoryHygieneService.listDormitoryHygiene(hygieneListReq);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @ApiOperation("删除指定宿舍卫生信息")
    @DeleteMapping(value = "/delDormitoryHygiene/{ids}")
    public CommonResult delDormitoryHygiene(@PathVariable Long[] ids) {
        dormitoryHygieneService.deleteByIds(ids);
        return CommonResult.success();
    }

    @ApiOperation("导出宿舍卫生统计信息")
    @PostMapping(value = "/exportDormitoryHygiene")
    public CommonResult export(HttpServletResponse response, @RequestBody DormitoryHygieneListReq hygieneListReq) {
        List<ExportDormitoryHygieneVO> dormitoryHygieneCountVOS = dormitoryHygieneService.getDormitoryHygieneCountExport(hygieneListReq);
        try {
            EasyExcel.write(response.getOutputStream(), ExportDormitoryHygieneVO.class).sheet("宿舍卫生统计").doWrite(dormitoryHygieneCountVOS);
            return CommonResult.success();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }
}
